var _GUISprite;
var _GUISpriteList = new Array();
var _GUIHead;
var _GUIHP;
var _GUIMP;
var _GUIHPText;
var _GUIMPText;
var _GUISkill = new Array();
var _GUIMenu = new Array();
var _GUIState;
var _GUIListNumber;
var _GUIAction;


// 初始化头像生命魔法槽等
function newHPMP(){
	_GUIAction = false;
	_GUIHead = new cc.Sprite();
	_GUIHP = new cc.Sprite(res.gc_item_png, cc.rect(0, 0, 200, 50));
	_GUIMP = new cc.Sprite(res.gc_item_png, cc.rect(0, 50, 200, 50));
	_GUIHead.setAnchorPoint(0.5, 0.5);
	_GUIHP.setAnchorPoint(0, 0.5);
	_GUIMP.setAnchorPoint(0, 0.5);
	_GUIHead.setPosition(172,252);
	_GUIHP.setPosition(70,960-825);
	_GUIMP.setPosition(70,960-875);
	_GUIHead.setLocalZOrder(10);
	_GUIHP.setLocalZOrder(10);
	_GUIMP.setLocalZOrder(10);
	g_GBTouchLayer.addChild(_GUIHead);
	g_GBTouchLayer.addChild(_GUIHP);
	g_GBTouchLayer.addChild(_GUIMP);
	_GUIHPText = new cc.LabelTTF("","res/font/Microsoft Yahei.ttf", 30);
	_GUIHPText.attr({
		anchorX : 0.5,
		anchorY : 0.5,
		x: 170,
		y: 960-825,
		color : cc.color(0, 0, 0)
	});
	_GUIMPText = new cc.LabelTTF("","res/font/Microsoft Yahei.ttf", 30);
	_GUIMPText.attr({
		anchorX : 0.5,
		anchorY : 0.5,
		x: 170,
		y: 960-875,
		color : cc.color(0, 0, 0)
	});
	_GUIHPText.setLocalZOrder(15);
	_GUIMPText.setLocalZOrder(15);
	g_GBTouchLayer.addChild(_GUIHPText);
	g_GBTouchLayer.addChild(_GUIMPText);
}

// 生命魔法槽刷新
function GUIRefrshHPMP(chess){
	_GUIHead.setTexture(chess.chess.pic);
	_GUIHead.setTextureRect(cc.rect((chess.chess.picno % 5)*150, parseInt(Math.floor(chess.chess.picno/5)*150), 150, 150));
	_GUIHP.setTexture(res.gc_item_png);
	_GUIHP.setTextureRect(cc.rect(0, 0, parseInt(200*(CSgetState("HP",chess.id)/CSgetState("HP上限",chess.id))), 50));
	_GUIMP.setTexture(res.gc_item_png);
	_GUIMP.setTextureRect(cc.rect(0, 50, parseInt(200*(CSgetState("MP",chess.id)/CSgetState("MP上限",chess.id))), 50));
	_GUIHPText.setString(CSgetState("HP",chess.id).toString()+"/"+CSgetState("HP上限",chess.id).toString());
	_GUIMPText.setString(CSgetState("MP",chess.id).toString()+"/"+CSgetState("MP上限",chess.id).toString());
}

function GUISetTagEnd(chess){
	if(chess!=null){
		chess.tag.setTextureRect(cc.rect(125, 100, 25, 25));
	}
}

function GUISetTagDelay(chess){
	if(chess!=null){
		chess.tag.setTextureRect(cc.rect(100, 100, 25, 25));
	}
}

function GUICleanTag(chess){
	if(chess!=null){
		chess.tag.setTextureRect(cc.rect(100, 125, 25, 25));
	}
}

function GUICleanAllTag(){
	for(var i in _GDChessList){
		if(_GDChessList[i] != null){
			_GDChessList[i].tag.setTextureRect(cc.rect(100, 125, 25, 25));
		}
	}
}

// 棋子状态随时刷新
function GUIRefrshChess(){
	for(var i in _GDChessList){
		_GDChessList[i].hPText.setString(CSgetState("HP",_GDChessList[i].id));
		_GDChessList[i].hitText.setString(CSgetState("攻击力",_GDChessList[i].id));
	}
}

// 显示当前目标
function GUIShowNowAction(){
	_GUIAction = true;
	var animation =new cc.Animation();
	animation.addSpriteFrame(new cc.SpriteFrame(res.gc_move_png, cc.rect(100, 100, 100, 100)));
	animation.addSpriteFrame(new cc.SpriteFrame(res.gc_move_png, cc.rect(200, 100, 100, 100)));
	animation.addSpriteFrame(new cc.SpriteFrame(res.gc_move_png, cc.rect(300, 100, 100, 100)));
	animation.setDelayPerUnit(0.15);
	animation.setRestoreOriginalFrame(true);
	_GUISprite = new cc.Sprite(res.gc_move_png, cc.rect(100, 100, 100, 100));
	_GUISprite.setPosition(CChessPositionx(_GDNowChess.position.x),CChessPositiony(_GDNowChess.position.y));
	_GUISprite.setLocalZOrder(100);
	g_GBTouchLayer.addChild(_GUISprite);
	_GUISprite.runAction(cc.RepeatForever(cc.animate(animation)));
};

function GUIHideNowAction(){
	if(_GUIAction){
		_GUIAction =false;
		g_GBTouchLayer.removeChild(_GUISprite);
	}
}

// 显示攻击目标
function GUIShowAttackTarget(){
	var animation =new cc.Animation();
	animation.addSpriteFrame(new cc.SpriteFrame(res.gc_move_png, cc.rect(400, 100, 100, 100)));
	animation.addSpriteFrame(new cc.SpriteFrame(res.gc_move_png, cc.rect(500, 100, 100, 100)));
	animation.addSpriteFrame(new cc.SpriteFrame(res.gc_move_png, cc.rect(600, 100, 100, 100)));
	animation.setDelayPerUnit(0.15);
	animation.setRestoreOriginalFrame(false);
	_GUISprite = new cc.Sprite(res.gc_move_png, cc.rect(100, 100, 100, 100));
	_GUISprite.setPosition(CChessPositionx(_GDway[1].x),CChessPositiony(_GDway[1].y));
	_GUISprite.setLocalZOrder(100);
	g_GBTouchLayer.addChild(_GUISprite);
	_GUISprite.runAction(cc.animate(animation));
};

// 显示技能目标
function GUIShowSkillTarget(position){
	var animation =new cc.Animation();
	animation.addSpriteFrame(new cc.SpriteFrame(res.gc_move_png, cc.rect(400, 100, 100, 100)));
	animation.addSpriteFrame(new cc.SpriteFrame(res.gc_move_png, cc.rect(500, 100, 100, 100)));
	animation.addSpriteFrame(new cc.SpriteFrame(res.gc_move_png, cc.rect(600, 100, 100, 100)));
	animation.setDelayPerUnit(0.15);
	animation.setRestoreOriginalFrame(false);
	_GUISprite = new cc.Sprite(res.gc_move_png, cc.rect(100, 100, 100, 100));
	_GUISprite.setPosition(CChessPositionx(position.x),CChessPositiony(position.y));
	_GUISprite.setLocalZOrder(99);
	g_GBTouchLayer.addChild(_GUISprite);
	_GUISprite.runAction(cc.animate(animation));
};

function GUIShowSkillIcon(chess){
	for(var i=0 ;i<4 ; i++){
		_GUIMenu[i].setVisible(false);
	}
	for(var i=0 ;i<6 ; i++){
		_GUISkill[i].setVisible(false);
	}
	for(var i in chess.skill){
		var skillX = parseInt(chess.skill[i].picno % 10);
		var skillY = parseInt(chess.skill[i].picno / 10);
		var positionX = parseInt(i % 2);
		var positionY = parseInt(i / 2);
		_GUISkill[i].initWithFile(chess.skill[i].pic,cc.rect(skillX*100,skillY*100,100,100));
		_GUISkill[i].setVisible(true);
	}
};

function GUIResetMenuIcon(){
	if((!_GDThisTurnNoMoveList.length)||_GDMoveDone)
		_GUIMenu[0].initWithFile("res/move.png",cc.rect(400,200,100,100));
	else
		_GUIMenu[0].initWithFile("res/move.png",cc.rect(0,200,100,100));
	_GUIMenu[1].initWithFile("res/move.png",cc.rect(100,200,100,100));
	_GUIMenu[2].initWithFile("res/move.png",cc.rect(200,200,100,100));
	_GUIMenu[3].initWithFile("res/move.png",cc.rect(300,200,100,100));
};

function GUIShowMenuIcon(){
	for(var i=0 ;i<6 ; i++){
		_GUISkill[i].setVisible(false);
	}
	for(var i=0 ;i<4 ; i++){
		_GUIMenu[i].setVisible(true);
	}
};

function GUIShowStateWindow(){
	_GUIState =  new cc.Sprite("res/state.png",cc.rect(0,0,640,960));
	_GUIState.attr({
		anchorX : 0,
		anchorY : 0,
		x: 0,
		y: 0,
	});
	_GUIState.setLocalZOrder(500);
	
	var temp = new cc.Sprite(_GDStateChess.chess.pic, cc.rect((_GDStateChess.chess.picno % 5)*150, parseInt(Math.floor(_GDStateChess.chess.picno/5)*150), 150, 150));
	temp.attr({x: 25,y: 960-25,anchorX : 0,anchorY : 1});
	temp.setLocalZOrder(1000);
	_GUIState.addChild(temp, 1000, "头像");
	temp = new cc.LabelTTF(CSgetState("名称", _GDStateChess.id), "res/font/Microsoft Yahei.ttf", 25);
	temp.attr({x: 280,y: 960-40,anchorX : 0,anchorY : 1,color : cc.color(0, 0, 0)});
	_GUIState.addChild(temp, 1000, "名称");
	temp = new cc.LabelTTF(CSgetState("称号", _GDStateChess.id), "res/font/Microsoft Yahei.ttf", 25);
	temp.attr({x: 280,y: 960-78,anchorX : 0,anchorY : 1,color : cc.color(0, 0, 0)});
	_GUIState.addChild(temp, 1000, "称号");
	temp = new cc.LabelTTF(CSgetState("攻击力", _GDStateChess.id).toString(), "res/font/Microsoft Yahei.ttf", 25);
	temp.attr({x: 280,y: 960-115,anchorX : 0,anchorY : 1,color : cc.color(0, 0, 0)});
	_GUIState.addChild(temp, 1000, "攻击力");
	temp = new cc.LabelTTF(CSgetState("射程", _GDStateChess.id).toString(), "res/font/Microsoft Yahei.ttf", 25);
	temp.attr({x: 280,y: 960-154,anchorX : 0,anchorY : 1,color : cc.color(0, 0, 0)});
	_GUIState.addChild(temp, 1000, "射程");
	temp = new cc.LabelTTF(CSgetState("移动力", _GDStateChess.id).toString(), "res/font/Microsoft Yahei.ttf", 25);
	temp.attr({x: 280,y: 960-192,anchorX : 0,anchorY : 1,color : cc.color(0, 0, 0)});
	_GUIState.addChild(temp, 1000, "移动力");
	temp = new cc.LabelTTF(CSgetState("速度", _GDStateChess.id).toString(), "res/font/Microsoft Yahei.ttf", 25);
	temp.attr({x: 280,y: 960-230,anchorX : 0,anchorY : 1,color : cc.color(0, 0, 0)});
	_GUIState.addChild(temp, 1000, "速度");
	
	temp = new cc.Sprite(res.gc_item_png, cc.rect(0, 0, 200, 50));
	temp.setAnchorPoint(0, 0.5);
	temp.setTextureRect(cc.rect(0, 0, parseInt(200*(CSgetState("HP",_GDStateChess.id)/CSgetState("HP上限",_GDStateChess.id))), 50));
	temp.setScale(0.75, 0.75);
	temp.setPosition(25,960-211);
	_GUIState.addChild(temp, 1000, "生命槽");
	temp = new cc.LabelTTF(CSgetState("HP",_GDStateChess.id).toString()+"/"+CSgetState("HP上限",_GDStateChess.id).toString(),"res/font/Microsoft Yahei.ttf", 20);
	temp.attr({anchorX : 0.5,anchorY : 0.5,x: 100,y: 960-212,color : cc.color(0, 0, 0)});
	_GUIState.addChild(temp, 1000, "生命值");
	
	temp = new cc.Sprite(res.gc_item_png, cc.rect(0, 50, 200, 50));
	temp.setAnchorPoint(0, 0.5);
	temp.setTextureRect(cc.rect(0, 50, parseInt(200*(CSgetState("MP",_GDStateChess.id)/CSgetState("MP上限",_GDStateChess.id))), 50));
	temp.setScale(0.75, 0.75);
	temp.setPosition(25,960-257);
	_GUIState.addChild(temp, 1000, "魔法槽");
	temp = new cc.LabelTTF(CSgetState("MP",_GDStateChess.id).toString()+"/"+CSgetState("MP上限",_GDStateChess.id).toString(),"res/font/Microsoft Yahei.ttf", 20);
	temp.attr({anchorX : 0.5,anchorY : 0.5,x: 100,y: 960-262,color : cc.color(0, 0, 0)});
	_GUIState.addChild(temp, 1000, "魔法值");
	
	for(var i in _GDStateChess.skill){
		var skillX = parseInt(_GDStateChess.skill[i].picno % 10);
		var skillY = parseInt(_GDStateChess.skill[i].picno / 10);
		temp = new cc.Sprite(_GDStateChess.skill[i].pic, cc.rect(skillX*100,skillY*100, 100, 100));
		temp.setAnchorPoint(0, 0);
		temp.setScale(0.5, 0.5);
		temp.setPosition(30,960-350-(108*i));
		_GUIState.addChild(temp, 1000, "技能"+i);
		
		temp = new cc.LabelTTF(_GDStateChess.skill[i].name,"res/font/Microsoft Yahei.ttf", 20);
		temp.attr({anchorX : 0,anchorY : 1,x: 110,y: 960-305-(108*i),color : cc.color(0, 0, 0)});
		_GUIState.addChild(temp, 1000, "技能名"+i);
		if(undefined != _GDStateChess.skill[i].cost){
			temp = new cc.LabelTTF("消耗："+_GDStateChess.skill[i].cost,"res/font/Microsoft Yahei.ttf", 20);
			temp.attr({anchorX : 0,anchorY : 1,x: 250,y: 960-305-(108*i),color : cc.color(0, 0, 0)});
			_GUIState.addChild(temp, 1000, "技能消耗"+i.toString());
		}
		temp = new cc.LabelTTF("　　　　"+_GDStateChess.skill[i].description,"res/font/Microsoft Yahei.ttf", 20,cc.size(375,70), cc.TEXT_ALIGNMENT_LEFT);
		temp.attr({anchorX : 0,anchorY : 1,x: 30,y: 960-330-(108*i),color : cc.color(0, 0, 0)});
		_GUIState.addChild(temp, 1000, "技能说明"+i);
	}
	
	var stateTexture = "";
	var effectList = CEOuterEffectGetter(_GDStateChess.id);
	var printList = new Array();
	for(var i in effectList){
		var effects = effectList[i].effect.split(",");
		var parameters = effectList[i].parameter.split(",");
		for(var j in effects){
			if(effects[j] == "持续"){
				if(parameters[j] == 0){
					printList.push(effectList[i]);
				}
			}
		}
	}
	while (printList.length != 0){
		var num = 1;
		var i = 0;
		stateTexture += printList[0].name;
		stateTexture += '\n';
		while(i < printList.length){
			if(i != 0 && printList[0].name == printList[i].name){
				num++;
				printList.splice(i,1);
			}
			else{
				i++;
			}
		}
		stateTexture += "叠加"+num+"层";
		stateTexture += '\n';
		stateTexture += printList[0].description;
		stateTexture += '\n';
		stateTexture += '\n';
		printList.splice(0,1);
	}
	for(var i in effectList){
		var effects = effectList[i].effect.split(",");
		var parameters = effectList[i].parameter.split(",");
		var skip = false;
		for(var j in effects){
			if(effects[j] == "持续"&&parameters[j] == 0){
				skip = true;
			}
		}
		if(skip == true){
			continue;
		}
		stateTexture += effectList[i].name;
		stateTexture += '\n';
		for(var j in effects){
			if(effects[j] == "持续"&&parameters[j] != 0){
				stateTexture += "剩余"+parameters[j]+"回合";
				stateTexture += '\n';
			}
		}
		stateTexture += effectList[i].description;
		stateTexture += '\n';
		stateTexture += '\n';
	}
	temp = new cc.LabelTTF(stateTexture, "res/font/Microsoft Yahei.ttf", 15,cc.size(180,900), cc.TEXT_ALIGNMENT_LEFT);
	temp.attr({x: 440,y: 960-40,anchorX : 0,anchorY : 1,color : cc.color(0, 0, 0)});
	_GUIState.addChild(temp, 1000, "状态");
	g_GBTouchLayer.addChild(_GUIState);
};

function GUIResetSkillIcon(){
	for(var i=0 ;i<6 ; i++){
		var positionX = parseInt(i % 2);
		var positionY = parseInt(i / 2);
		_GUISkill[i].setPosition(400+(100*positionX),290-(positionY*100));
		_GUISkill[i].setLocalZOrder(5);
	}
};